package au.com.mason.propertyTracker.activator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import au.com.mason.propertyTracker.domain.Property;
import au.com.mason.propertyTracker.dto.ChangedProperties;
import au.com.mason.propertyTracker.service.FindSoldUnderContractService;
import au.com.mason.propertyTracker.util.ApplicationContextUtils;

public class FindSoldUnderContractServiceActivator {
	private static final Log LOG = LogFactory.getLog(FindSoldUnderContractServiceActivator.class);
	
	public static void main(String[] args) {
		ChangedProperties changedProperties = new ChangedProperties();
		FindSoldUnderContractService findSoldUnderContractService = (FindSoldUnderContractService) ApplicationContextUtils.getBean("findSoldUnderContractService");
	
		List<String> messages = new ArrayList<String>();
		
		try {
			findSoldUnderContractService.findProperties(changedProperties);
			findSoldUnderContractService.findRentals(changedProperties);
			messages = findSoldUnderContractService.findNewRentals();
		}
		catch (Exception e) {
			System.out.println("Exception - " + e.getMessage());
			//e.printStackTrace();
		}
		finally {
			logResults(changedProperties, messages);	
		}
	}
	
	private static void logResults(ChangedProperties changedProperties, List<String> messages) {
		LOG.info("****************************** Results for - " + new Date());
		
		LOG.info("****************************** Removed Properties ");
		for (Property property : changedProperties.getRemovedProperties()) {
			logProperty(property);
		}
		LOG.info("****************************** Removed Rentals ");
		for (Property property : changedProperties.getRemovedRentals()) {
			logProperty(property);
		}
		LOG.info("****************************** Changed Price ");
		for (Property property : changedProperties.getPropertiesChangedPrice()) {
			logProperty(property);
		}
		LOG.info("****************************** Under Contract ");
		for (Property property : changedProperties.getUnderContractProperties()) {
			logProperty(property);
		}
		LOG.info("****************************** Sold Properties ");
		for (Property property : changedProperties.getSoldProperties()) {
			logProperty(property);
		}
		LOG.info("****************************** Changed Properties ");
		for (Property property : changedProperties.getChangedProperties()) {
			logProperty(property);
		}
		
		for (String message : messages) {
			LOG.info(message);
		}
	}
	
	private static void logProperty(Property property) {
		String suburbName = "";
		//if (property.getSuburb() != null) suburbName = property.getSuburb().getName();
		LOG.info(property.getStreetAddress() + ", " + suburbName);
	}

}
